package com.vipboot.framework.web.handler;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.vipboot.framework.core.domain.R;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.comm.exception.SmsBlendException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * SMS异常处理器
 */
@Slf4j
@ConditionalOnClass(value = {SmsBlendException.class})
@RestControllerAdvice
@Order(1)
public class SmsExceptionHandler {

    /**
     * sms异常
     */
    @ExceptionHandler(SmsBlendException.class)
    public R<Void> handleSmsBlendException(SmsBlendException e, HttpServletRequest request) {
        String requestURI = request.getRequestURI();
        log.error("请求地址 {} ,发生sms短信异常.", requestURI);
        log.error(ExceptionUtil.stacktraceToString(e));
        return R.fail("短信发送失败，请稍后再试...");
//        return R.fail(HttpStatus.HTTP_INTERNAL_ERROR, "短信发送失败，请稍后再试...");
    }

}
